<?php

declare(ENCODING = 'UTF-8');

/*                                                                        *
 * This script is part of the TYPO3 project - inspiring people to share!  *
 *                                                                        *
 * TYPO3 is free software; you can redistribute it and/or modify it under *
 * the terms of the GNU General Public License version 2 as published by  *
 * the Free Software Foundation.                                          *
 *                                                                        *
 * This script is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *	
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
 * Public License for more details.                                       *
 *                                                                        */	

/**
 * @package auxo
 * @subpackage Web
 * @version $Id$
 */

/**
 * The Web Request 
 * 
 * @package auxo
 * @subpackage Web	
 * @version $Id$	
 * @copyright Copyright belongs to the respective authors
 * @author Andreas Horn
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
 */
class tx_auxo_Request {
	/**
	 * HTTP Method Get
	 *
	 */
	
	const METHOD_GET  = 'GET';
	/**
	 * HTTP Method Put
	 *
	 */
	
	const METHOD_PUT  = 'PUT';
	/**
	 * HTTP Method Post
	 *
	 */
	
	const METHOD_POST = 'POST';
	/**
	 * HTTP Method Head
	 *
	 */
	
	const METHOD_HEAD = 'HEAD';
	
	/**
	 * HTTP Method Delete
	 *
	 */
	const METHOD_DELETE = 'DELETE';
	
	/**
	 * An array object that holds all request parameters 
	 *
	 * @var arrayObject
	 */
	protected $parameters = NULL;
	
	/**
	 * Extension Environment Instance
	 *
	 * @var tx_auxo_ExtensionEnvironment
	 */
	protected $environment = NULL;
	
	/**
	 * Creates a new Request
	 *
	 * @param tx_auxo_ExtensionEnvironment $environment
	 */
	public function __construct(tx_auxo_ExtensionEnvironment $environment) {
		$this->parameters = new ArrayObject(t3lib_div::GParrayMerged($environment->getExtension()));
		// Initialize the cHash system if there are parameters available
		if ($GLOBALS['TSFE'] && count($this->parameters)) {
			$GLOBALS['TSFE']->reqCHash();
		}		
	}
	
	/**
	 * Returns name of request method. There are get, post, put, delete and head
	 *
	 * @return stirng $method
	 */
	public function getMethod() {
		return isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : self::METHOD_GET;
	}
	
	/**
	 * Returns all request parameters
	 *
	 * @return tx_auxo_arrayObject $parameters
	 */
	public function getParameters() {
		return $this->parameters;
	}
	
	/**
	 * Returns current page Id
	 * 
	 * @return int $pageId Page Id
	 */
	public function getPageId() {
		return $GLOBALS['TSFE']->id;
	}
	
	/**
	 * Returns all content types
	 * 
	 * @return array list of content types
	 */
	public function getContentTypes() {
		$parts = explode(',', $_SERVER['HTTP_ACCEPT']);
		$types = array();
		
		foreach ($parts as $part) {
			if (($pos = strpos($part, ';'))) {
				$types[] = trim(substr($part, 0, $pos));
			}
			else {
				$types[] = $part;
			}
		}
		return array_values($types);
	}
	
   /**
     * Moves an uploaded file into either the extension upload directory 
     * or if $target is an absolute path there.
	 *
	 * @param string $source source filename
	 * @param string $target target path 
	 * @throws tx_auxo_IOexception if a directory can not created
	 * @throws tx_auxo_IOexception if a file can not be moved successfully
	 */
	public function moveUploadedFileTo($source, $target) {
		$filemode = octdec('0755');

		if ($filename[0] <> '/') {
			$path = t3lib_div::getFileAbsFileName('uploads/'. $this->environment->getExtension(), '/');
			if(!is_dir($path)){
				if(!mkdir($path)){
					// error handling in case of makedir errors
					throw new tx_auxo_IOexception(sprintf('Can not create directory %s', $path));
				}
				else {
					@chmod($path, $filemode);
				}
			}
		}

		$filepath = $path . $target;
		if(move_uploaded_file($_FILES[$this->environment->getExtension()]['tmp_name'][$source], $filepath)) {
			// successfull moved to target directory and renamed
			@chmod($filepath, $filemode);
		} else {
			throw new tx_auxo_IOexception(sprintf('Can not move uploaded file %s', $source));
		}
	}
	
	/**
	 * Returns the value of a client cookie
	 *
	 * @param string $name name of a cookie
	 * @return mixed $value cookie value or null if not found
	 */
	public function getCookie($name) {
		return $_COOKIE[$name];
	}
}
?>